package cn.changein.framework.util.aliyun;

import cn.changein.framework.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

import java.util.Map;
import java.util.Objects;

/**

 * @version V1.0
 * @Description: TODO(短信)
 * @Title: AliYunUtils
 * @Package cn.changein.framework.util
 * @date 2021/1/20
 */
@Slf4j
public class AliYunUtils {

    /**
     * @Description: 阿里云发送短信
     * @Date:2021/1/21 9:25
     * @Param: [aliYunSendSmsParam]

     */
    public static ResponseData sendSms(AliYunSendSmsParam aliYunSendSmsParam) {

        String accessKeyId = aliYunSendSmsParam.getAccessKeyId();
        String secret = aliYunSendSmsParam.getAccessKeySecret();
        String regionId = aliYunSendSmsParam.getRegionId();
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        String phoneNumbers = aliYunSendSmsParam.getPhoneNumbers();
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction(aliYunSendSmsParam.getSysAction()); //"SendSms"
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", aliYunSendSmsParam.getSignName());
        request.putQueryParameter("TemplateCode", aliYunSendSmsParam.getTemplateCode()); //
        request.putQueryParameter("TemplateParam", aliYunSendSmsParam.getTemplateParam()); //{"code":"999"}
        try {
            CommonResponse response = client.getCommonResponse(request);
            int httpStatus = response.getHttpStatus();
            //{"RequestId":"D8194A8D-FCDD-4B66-9FCC-1EA7B18DA4CE","Message":"OK","BizId":"579515811192828440^0","Code":"OK"}
            //200
            if (!Objects.equals(httpStatus, HttpStatus.OK.value())) {
                return ResponseData.error("请求失败");
            }
            String data = response.getData();
            log.info("sendSms result phone={} data={}", phoneNumbers, data);
            Map<String, Object> map = JSONObject.parseObject(data, Map.class);
            String code = (String) map.get("Code");
            String message = (String) map.get("Message");
            String reasonPhrase = HttpStatus.OK.getReasonPhrase();
            if (StringUtils.equals(code, reasonPhrase) && StringUtils.equals(message, reasonPhrase)) {
                return ResponseData.success(message);
            }
            return ResponseData.error(message);
        } catch (Exception e) {
            log.error("error={}", e.getMessage());
            return ResponseData.error(e.getMessage());
        }
    }

    public static void main(String[] args) {
        AliYunSendSmsParam param = new AliYunSendSmsParam();
        param.setAccessKeyId("");
        param.setAccessKeySecret("");
        param.setRegionId("cn-hangzhou");
        param.setPhoneNumbers("");
        param.setSignName("");
        param.setTemplateCode("SMS_142382969");
        param.setTemplateParam("{\"code\":\"888\"}");
        AliYunUtils.sendSms(param);
    }
}
